package com.qf.tms.service.impl;


import com.qf.tms.entity.Classroom;
import com.qf.tms.entity.Student;
import com.qf.tms.entity.StudentVO;
import com.qf.tms.entity.Subjects;
import com.qf.tms.mapper.ClassroomMapper;
import com.qf.tms.mapper.StudentMapper;
import com.qf.tms.service.StudentServiceV2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class StudentServiceImplV2 implements StudentServiceV2 {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClassroomMapper classroomMapper;

    @Override
    public List<StudentVO> selectStuAndCmAllV2() {
        //存储最终的学生信息
        List<StudentVO> list = new ArrayList<>();


        //查询所有学生
        List<Student>  studentList = studentMapper.select();
        studentList.forEach(student -> {
            //根据每次遍历出的对象获取cid
            int cid = student.getCid();
            //或取教室信息
            Classroom classroom = classroomMapper.findClassroomById(cid);
            StudentVO studentVO = new StudentVO();
            //将student对象的信息赋值给studentVO对象
            BeanUtils.copyProperties(student,studentVO);
            //为StudentVO对象设置教室信息
            studentVO.setClassroom(classroom);
            //将studentVO对象存入集合中
            list.add(studentVO);
        });

        return list;
    }

    @Override
    public StudentVO findStuSub(int stuId) {
        List<StudentVO> list = new ArrayList<>();

        //根据id查找学生信息
        Student student = studentMapper.findStuById(stuId);
        List<Subjects> subjectsList = new ArrayList<>();
        //查询学生的学科信息编号
        List<Subjects> subsList= studentMapper.findStuSubjectAll(stuId);
        //遍历subjectsList获取学科信息
        subsList.forEach(subjects -> {
            //获取每一个学科编号
            int subId = subjects.getSubId();
            Subjects subject = studentMapper.findSubjectById(subId);
            subjectsList.add(subject);
        });

        StudentVO studentVO = new StudentVO();
        BeanUtils.copyProperties(student,studentVO);
        studentVO.setSubList(subjectsList);


        return studentVO;
    }
}
